﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.DataAccess.Repository;
using ExaminationSys.Helper;
using Ext.Net;
using ListItem = Ext.Net.ListItem;

namespace ExaminationSys.View.Section
{
    public partial class SubSectionInfoListEditWindow : System.Web.UI.UserControl
    {
        private static readonly SubSectionDetailInfoService Proxy = ObjectFactory<SubSectionDetailInfoService>.Create();
        private static readonly PaperInfoService PaperProxy = ObjectFactory<PaperInfoService>.Create();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!X.IsAjaxRequest){
                //SetData(IDHidden.Value, ParentIDHidden.Value);
            }

        }

        public Store GridStore { get; set; }
        public string ID { get; set; }

        public void SetData(string id,string sectionId)
        {
            if (!string.IsNullOrEmpty(id))
            {
               
                var item = Proxy.Get(new Guid(id));
                if (item != null)
                {
                    DetailForm_TB_Name.Text = item.Name;
                    TB_Content.Text = item.Content;
                    SelectBoxSubSectionType.Select(item.KnowledgeType - 1);
                    NumberOrderIndex.Number = item.OrderIndex;
                    Knowledge.Text = item.KnowledgeKeyWords;
                    var paper = PaperProxy.Get(item.PaperID);
                    if(paper!=null)
                    {
                        TB_Paper.Text = paper.Title;
                        HidPaperId.Value = paper.ID.ToString();
                    }
                    ID = id;
                    X.Js.Call("setID", id,item.PaperID,item.SubSectionID);
                }
                else
                {

                    ID = id;
                    X.Js.Call("setID", id, "", sectionId);
                }
            }
            else
            {
                DetailForm_TB_Name.Text = "";
                HidPaperId.Value = "";
                TB_Paper.Text = "";
                Knowledge.Text = "";
                TB_Content.Text = "";
                SelectBoxSubSectionType.Select(0);
                NumberOrderIndex.Number = 1;
                EditNodeWindow.Title = "新增文章";
                ID = id;
                X.Js.Call("setID", id, "", sectionId);
            }
        }

        public void Show()
        {
           // tbEditor.Hidden = false;
            this.EditNodeWindow.Show();
        }

        public void ImageButtonSearch_Click(object sender, DirectEventArgs e)
        {
            TB_Paper.Text = "test";
            MessageBoxHelper.ShowInfo("test");
        }

        public void SaveNode_Click(object sender, DirectEventArgs e)
        {
            var idObj = IDHidden.Value;
            var paperIdObj = HidPaperId.Value;
            var name = DetailForm_TB_Name.Text.Trim();
            var knowledge = Knowledge.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                MessageBoxHelper.ShowError("名称不能为空");
                return;
            }
            if (string.IsNullOrEmpty(knowledge))
            {
                MessageBoxHelper.ShowError("知识点不能为空");
                return;
            }
            var sectionId = HidSectionId.Value;
            if (string.IsNullOrEmpty(sectionId))
            {
                MessageBoxHelper.ShowError("子栏目ID获取失败");
                return;
            }


            var selectItem = SelectBoxSubSectionType.SelectedItem;
            var sectionType = selectItem == null ? -1 : Convert.ToInt32(selectItem.Value);

            if (sectionType == -1)
            {
                MessageBoxHelper.ShowError("请选择类型");
                return;
            }

            if (sectionType == 2||sectionType==3)
            {
                if (string.IsNullOrWhiteSpace(paperIdObj))
                {
                    MessageBoxHelper.ShowError("测试和练习类型必须选择试卷");
                    return;
                }
            }

            var content = TB_Content.Text;
            if (string.IsNullOrWhiteSpace(content))
            {
                MessageBoxHelper.ShowError("内容不能为空");
                return;
            }


            var id = Guid.NewGuid();

            var isAdd = string.IsNullOrWhiteSpace(idObj);
            if (!string.IsNullOrWhiteSpace(idObj))
            {
                id = new Guid(idObj);
            }

            var op = FormsAuthenticationHelper.GetCurrentUserID();
            if (string.IsNullOrEmpty(op))
            {
                FormsAuthenticationHelper.SignOut();
                Response.Redirect("~/Account/Login.aspx");
            }

            var item = isAdd ? new SubSectionDetailInfo() : Proxy.Get(id);
            item.ID = isAdd ? id : item.ID;
            item.Name = name;
            item.SubSectionID = new Guid(sectionId);
            item.KnowledgeType = sectionType;
            item.PaperID = string.IsNullOrWhiteSpace(paperIdObj) ? Guid.Empty : new Guid(paperIdObj);
            var orderIndexObj = NumberOrderIndex.Number;
            int latestIndex = 0;
            if (orderIndexObj >= 0)
                latestIndex = Convert.ToInt32(orderIndexObj);
            item.OrderIndex = latestIndex;
            item.KnowledgeKeyWords = knowledge;
            item.Content = content;
            item.Lock = false;
            item.Status = 1;
            item.Opeator = op;
            item.OpeatorTime = DateTime.Now;

            bool success = false;
            if (isAdd)
            {
                var retCode = Proxy.Insert(item);
                success = retCode != null;
            }
            else
            {

                success = Proxy.Update(item);
            }
            if (success)
            {
                GridStore.Reload();
                MessageBoxHelper.ShowInfo("保存成功");
                EditNodeWindow.Hide();
            }
            else
            {
                MessageBoxHelper.ShowError("保存失败");
                return;
                
            }
        }

    }
}